import request from '@/utils/request'
import type { SingleUploadResponse, MultipleUploadResponse } from '@/types/upload'

// 上传头像
export const uploadAvatar = (file: File, onProgress?: (percentage: number) => void) => {
  const formData = new FormData()
  formData.append('avatar', file)

  return request.post<SingleUploadResponse>('/upload/avatar', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
    onUploadProgress: (progressEvent) => {
      if (progressEvent.total && onProgress) {
        const percentage = Math.round((progressEvent.loaded * 100) / progressEvent.total)
        onProgress(percentage)
      }
    },
  })
}

export const uploadAvatarWithUserId = (
  file: File,
  userId: number,
  onProgress?: (percentage: number) => void,
) => {
  const formData = new FormData()
  formData.append('avatar', file)

  return request.post<SingleUploadResponse>(`/upload/user/${userId}/avatar`, formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
    onUploadProgress: (progressEvent) => {
      if (progressEvent.total && onProgress) {
        const percentage = Math.round((progressEvent.loaded * 100) / progressEvent.total)
        onProgress(percentage)
      }
    },
  })
}

// 上传婚纱
export const uploadWeddings = (files: File[], onProgress?: (percentage: number) => void) => {
  const formData = new FormData()
  files.forEach((file) => {
    formData.append('wedding', file)
  })

  return request.post<MultipleUploadResponse>('/upload/weddings', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
    onUploadProgress: (progressEvent) => {
      if (progressEvent.total && onProgress) {
        const percentage = Math.round((progressEvent.loaded * 100) / progressEvent.total)
        onProgress(percentage)
      }
    },
  })
}

// 上传客照样片
export const uploadSamples = (files: File[], onProgress?: (percentage: number) => void) => {
  const formData = new FormData()
  files.forEach((file) => {
    formData.append('sample', file)
  })

  return request.post<MultipleUploadResponse>('/upload/samples', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
    onUploadProgress: (progressEvent) => {
      if (progressEvent.total && onProgress) {
        const percentage = Math.round((progressEvent.loaded * 100) / progressEvent.total)
        onProgress(percentage)
      }
    },
  })
}

// 上传套餐图片
export const uploadPackageImages = (files: File[], onProgress?: (percentage: number) => void) => {
  const formData = new FormData()
  files.forEach((file) => {
    formData.append('package', file)
  })

  return request.post<MultipleUploadResponse>('/upload/packages', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
    onUploadProgress: (progressEvent) => {
      if (progressEvent.total && onProgress) {
        const percentage = Math.round((progressEvent.loaded * 100) / progressEvent.total)
        onProgress(percentage)
      }
    },
  })
}

// 上传评价图片
export const uploadReviews = (files: File[], onProgress?: (percentage: number) => void) => {
  const formData = new FormData()
  files.forEach((file) => {
    formData.append('review', file)
  })

  return request.post<MultipleUploadResponse>('/upload/reviews', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
    onUploadProgress: (progressEvent) => {
      if (progressEvent.total && onProgress) {
        const percentage = Math.round((progressEvent.loaded * 100) / progressEvent.total)
        onProgress(percentage)
      }
    },
  })
}

// 上传 Banner
export const uploadBanners = (files: File[], onProgress?: (percentage: number) => void) => {
  const formData = new FormData()
  files.forEach((file) => {
    formData.append('banner', file)
  })

  return request.post<MultipleUploadResponse>('/upload/banners', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
    onUploadProgress: (progressEvent) => {
      if (progressEvent.total && onProgress) {
        const percentage = Math.round((progressEvent.loaded * 100) / progressEvent.total)
        onProgress(percentage)
      }
    },
  })
}
